package homework;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

//作业4：
//在你生成的文件中，生成10个男生或者10个女生的姓名.
//被点到的学生的姓名不会再被点到，如果都点完了，才开始第二轮点名
public class Test13 {
	static File path1 = new File("D:\\FileTest\\name.txt");
	static File path2 = new File("D:\\FileTest\\nowList.txt");
	
	public static void main(String[] args) throws IOException {
		ArrayList<String> textLine = new ArrayList<>();
		readText(textLine,path1);
		if(!path2.exists()) path2.createNewFile();
		ArrayList<String> nowLine = new ArrayList<>();
		readText(nowLine,path2);
		Random rd = new Random();
		FileWriter fw= new FileWriter(path2);
		if(nowLine.isEmpty()){
			Map<Integer,String> map = new HashMap<>();
			while(true){
				if(map.size()==10)break;
				int idx = rd.nextInt(100);
				if(!map.containsKey(idx)){
					map.put(idx, textLine.get(idx));
				}	
			}
			
			fw.write("0");
			fw.write("\n");
			for(Entry<Integer, String> entry:map.entrySet()){
				fw.write(entry.getValue()+"-1");
				fw.write("\n");
			}
			System.out.println("初始化10人份点名表成功");
		}
		else{
			System.out.println(nowLine);
			while(true){
				String num = nowLine.get(0);
				boolean flag = false;
				if((Integer.parseInt(num)+1)==11){
					for(String ls:nowLine){
						if(!flag){
							fw.write("0");
							fw.write("\n");
							flag = true;
							continue;
						}
						String[] arr1 = ls.split("-");
						fw.write(arr1[0]+"-"+arr1[1]+"-"+arr1[2]+"-"+arr1[3]+"-1");
						fw.write("\n");
					}
					System.out.println("已全部点名初始化成功");
					break;
				}
				int rdName = rd.nextInt(10)+1;
				String[] arr = nowLine.get(rdName).split("-");
				if(Integer.parseInt(arr[4])==0)continue;
				System.out.println("第"+(Integer.parseInt(num)+1)+"次点名");
				System.out.println(arr[0]);

				for(String ls:nowLine){
					if(!flag){
						String str = String.valueOf(Integer.parseInt(num)+1);
						fw.write(str);
						fw.write("\n");
						flag = true;
						continue;
					}
					if(--rdName==0){
						fw.write(arr[0]+"-"+arr[1]+"-"+arr[2]+"-"+arr[3]+"-0");
						fw.write("\n");
						continue;
					}
					fw.write(ls);
					fw.write("\n");
				}
				break;
			}
		}
		fw.close();
		
	}

	private static void readText(ArrayList<String> readLine, File path) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(path));
		String line="";

		while((line=br.readLine())!=null){
			if(line.length()>0)readLine.add(line);
		}
		br.close();
	}
}
